package com.abysscore.yachtdesign;

import java.io.File;
import java.io.Serializable;
import java.awt.Color;

import javax.swing.JDialog;

/*
 * {---------------------------------------------------------------------------------------------------}
     {                                       TFreePreferences                                            }
     {                                                                                                   }
     {   Container class for all program settings                                                        }
     {---------------------------------------------------------------------------------------------------}
 */
public class TFreePreferences implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private TFreeShip FOwner;
	private int FPointSize; // Half width of controlpoints in pixels when drawn on screen
     // Colors
	private Color FButtockColor;
	private Color FWaterlineColor;
	private Color FStationColor;
	private Color FDiagonalColor;
	private Color FEdgeColor;   // Color of normal edges
	private Color FCreaseColor;   // color of crease edges
	private Color FCreaseEdgeColor;   // color of crease control-edges
	private Color FGridColor;   // Color of gridlines
	private Color FGridFontColor;   // Color of font with gridlines
	private Color FCreasePointColor;   // Color of crease vertices
	private Color FRegularPointColor;   //
	private Color FCornerPointColor;   // Color of cornerpoints and points with at least 3 crease edges
	private Color FDartPointColor;   //
	private Color FSelectColor;   // Color of selected items
	private Color FLayerColor;   // Default color for new layers
	private Color FNormalColor;   // color of surface normals
	private Color FUnderWaterColor;   // Default color used for shading underwaterpart of the vessel
	private Color FViewportColor;
	private Color FLeakPointColor;
	private Color FMarkerColor;
	private Color FCurvaturePlotColor;
	private Color FControlCurveColor;
	private Color FHydrostaticsFontColor;
	private Color FZebraStripeColor;
	private File FOpenDirectory;   // Default directory to open existing files
	private File FSaveDirectory;   // Default directory to save files
	private File FImportDirectory;   // Default directory to import files
	private File FExportDirectory;   // Default directory to export files
	private String FLanguageFile;
	private int FMaxUndoMemory;  // Max. amount of allowable undo memory in megabytes
	private TFreePreferencesDialog Dialog;
	
	private File FGetExportDirectory()
	{
		File result = new File("FExportDirectory");
		if (result.exists());
		else 
			result.mkdir();
		return result;
	}
	private File FGetImportDirectory()
	{
		File result = new File("FImportDirectory");
		if (result.exists());
		else 
			result.mkdir();
		return result;
	}
	private File FGetOpenDirectory()
	{
		File result = new File("FOpenDirectory");
		if (result.exists());
		else 
			result.mkdir();
		return result;
	}
	private File FGetSaveDirectory()
	{
		File result = new File("FSaveDirectory");
		if (result.exists());
		else 
			result.mkdir();
		return result;
	}
	private void FSetViewporColor(Color Val)
	{
		int I;
		FViewportColor = Val;
		for (I=1; I < FOwner.NumberOfViewports(); I++)
			FOwner.Viewport(I-1).Color(FViewportColor);
	}
	
	public void Clear()
	{
		   ResetColors();
		   FPointSize = 2;
		   //TODO Get System home directory path
		   FOpenDirectory = new File("~/");
		   FSaveDirectory = new File("~/");
		   FImportDirectory = new File("~/");
		   FExportDirectory = new File("~/");
		   FLanguageFile = "English";
		   FMaxUndoMemory = 20;// Max 20Mb undomemory
	}
	
	public TFreePreferences(TFreeShip Owner)
	{
		FOwner = Owner;
		Clear();
	}
	
	private void Add(File FileName)
	{
		String Tmp;
		int I;
		boolean Found;
		I = FileName.getName().lastIndexOf(".");
		Tmp = FileName.getName().substring(0,I);
		Found = false;
		for (I = 1; I < Dialog.ComboBox1.getItemCount(); I++)
			if (Tmp.compareTo(Dialog.ComboBox1.getItemAt(I-1).toString())==0)
	        {
				Found = true;
	         	break;
	        }
		if (!Found)
			Dialog.ComboBox1.addItem(Tmp);
	}
	
	private void Browse(File Dir)
	{
		//TSearchRec SearchRec;
		//String[] SearchRec;
		String[] DirList;
		
		if (Dir.isDirectory()) 
		{	//Dir = Dir+'\';
			DirList = Dir.list();
			for (int i=0; i < DirList.length; i++)
			{
				if(DirList[i].toLowerCase().regionMatches(DirList[i].length()-4, ".ini", 0, 4))
					Add(new File(DirList[i]));
			}
			/*
			if (FindFirst(Dir+"*.ini",faAnyfile,SearchRec)==0)
			{
				if (Uppercase(ExtractFileExt(SearchRec.Name))=='.INI')
					Add(Searchrec.Name);
				while (FindNext(SearchRec)==0)
				{
					if (SearchRec.Name<>'.' && SearchRec.Name<>"..") 
						if (Uppercase(ExtractFileExt(SearchRec.Name))='.INI')
							Add(SearchRec.Name);
				}
				SysUtils.FindClose(SearchRec);
			}*/
		}
	}
	public void Edit()
	{
		TFreePreferencesDialog Dialog;
		String Filename;
		int I;	
		File Dir;
		/*	
		Dialog = new TFreePreferencesDialog(Owner);
		ShowTranslatedValues(Dialog);
		Dir = ExtractFileDir(Application.ExeName)+"Languages";
		Dialog.ComboBox1.removeAllItems();
		Dialog.ComboBox1.addItem("English");
		Browse(Dir);
		//Dialog.ComboBox1.ItemIndex:=0;
		for (I=1; I<Dialog.ComboBox1.getComponentCount();I++)
		{
			if (((String)Dialog.ComboBox1.Items[I-1])).equalsIgnoreCase(LanguageFile))==0)
			{
				Dialog.ComboBox1.ItemIndex = I-1;
				break;
			}
		}

		if (Dialog.Execute(Owner))
		{
			PointSize(Dialog.TrackBar1.Position);
			LayerColor(Dialog.Panel2.Color);
			UnderWaterColor(Dialog.Panel5.Color);
			EdgeColor(Dialog.Panel6.Color);
			CreaseEdgeColor(Dialog.Panel7.Color);
			CreaseColor(Dialog.Panel8.Color);
			RegularPointColor(Dialog.Panel9.Color);
			CreasePointColor(Dialog.Panel10.Color);
			CornerPointColor(Dialog.Panel11.Color);
			DartPointColor(Dialog.Panel12.Color);
			SelectColor(Dialog.Panel13.Color);
			GridColor(Dialog.Panel14.Color);
			GridFontColor(Dialog.Panel15.Color);
			StationColor(Dialog.Panel16.Color);
			ButtockColor(Dialog.Panel17.Color);
			WaterlineColor(Dialog.Panel18.Color);
			NormalColor(Dialog.Panel19.Color);
			DiagonalColor(Dialog.Panel20.Color);
			LeakPointColor(Dialog.Panel21.Color);
			MarkerColor(Dialog.Panel22.Color);
			CurvaturePlotColor(Dialog.Panel23.Color);
			ControlCurveColor(Dialog.Panel24.Color);
			HydrostaticsFontColor(Dialog.Panel25.Color);
			ZebraStripeColor(Dialog.Panel26.Color);
			ViewportColor(Dialog.Panel4.Color); // Set viewportcolor last, because it forces a repaint
			Filename = Dialog.Combobox1.Text;
			if(Uppercase(Filename)!=Uppercase(LanguageFile))
			{
				if (FileExists(ExtractFilePath(Application.ExeName)+"Languages"+Filename+".ini"))
				{
					LoadLanguage(Filename);
					LanguageFile = Filename;
					for(I=1; I<Application.ComponentCount; I++)
					{
						if (Application.Components[I-1] == TCustomForm)
							ShowTranslatedValues(Application.Components[I-1]);
					}
					for (I=1; I<Application.MainForm.MDIChildCount; I++)
					{
						if(Application.Mainform.MDIChildren[I-1] == TFreeLinesplanForm)
						{                  
							ShowTranslatedValues(TFreeLinesplanForm(Application.Mainform.MDIChildren[I-1]).LinesplanFrame);
						}
						else 
							ShowTranslatedValues(Application.Mainform.MDIChildren[I-1]);
					}         
				}
				else 
					LanguageFile = Filename;
				FMaxUndoMemory = Dialog.FreeNumInput1.AsInteger;
				if (assigned(Owner.FOnFileChanged))
					Owner.FOnFileChanged(Owner);
				if (assigned(Owner.FOnUpdateUndoData))
					Owner.FOnUpdateUndoData(Owner);
				if (assigned(Owner.FOnUpdateRecentFileList))
					Owner.FOnUpdateRecentFileList(Owner);
				if (assigned(Owner.FOnChangeCursorIncrement))
					Owner.FOnChangeCursorIncrement(Owner);
				if (assigned(Owner.FOnUpdateGeometryInfo))
					Owner.FOnUpdateGeometryInfo(owner);
				Owner.Redraw();
				Save();
			}
			Dialog.Destroy();
		}*/
	}
	
	public void Load()
	{
		;
	}
	public void ResetColors()
	{
		FButtockColor = new Color(0x8,0x80,0x040);     // Kind of teal-blue$00808040; 
	   	FWaterlineColor = new Color(0x8,0x80,0x40);    // Kind of teal-blue$00808040; 
	   	FStationColor = new Color(0x8,0x80,0x40);   // Kind of teal-blue $00808040; 
	   	FDiagonalColor = new Color(0x8,0x80,0x40);     // Kind of teal-blue
	   	FCreaseColor = Color.BLACK;        // color of descendants from crease controledges
	   	FCreaseEdgeColor = Color.RED;      // Color of crease control edges
	   	FGridColor = Color.GRAY;
	   	FGridFontColor = Color.WHITE;
	   	FEdgeColor = new Color(0x6F,0x6F,0x6F);
	   	FCreasePointColor = new Color(0x0, 0x40,0x80);
	   	FRegularPointColor = new Color(0xE1,0xE1,0xE1);
	   	FCornerPointColor = new Color(0xB9,0x5C,0x0);
	   	FDartPointColor = Color.PINK;
	   	FSelectColor = Color.YELLOW;
	   	FLayerColor = new Color(0,128,0);       // Default color of each layer (green-ish)
	   	FViewportColor = new Color(0x9F,0x9F,0x9F);
	   	FUnderWaterColor = new Color(240,240,240);
	   	FLeakPointColor = new Color(0x99,0xFF,0x0);
	   	FNormalColor = Color.WHITE;
	   	FMarkerColor = new Color(0x80,0x0,0xFF);
	   	FCurvaturePlotColor = Color.PINK;
	   	FControlCurveColor = new Color(0x71,0x18,0x8);
	   	FHydrostaticsFontColor = Color.GREEN;
	   	FZebraStripeColor = new Color(230,230,230);
	}
	public void Save()
	{
		;
	}
	
	public TFreeShip Owner() { return FOwner; }
	public void Owner(TFreeShip val) { FOwner = val; }
     // General options
	public int PointSize() { return FPointSize; }
	public void PointSize(int val) { FPointSize = val; }
     // Color settings
	public Color ButtockColor() { return FButtockColor; }
	public void ButtockColor(Color val) { FButtockColor=val; }
	public Color ControlCurveColor() { return FControlCurveColor; }
	public void ControlCurveColor(Color val) { FControlCurveColor=val; }
	public Color CornerPoinColor() { return FCornerPointColor; }
	public void CornerPoinColor(Color val) { FCornerPointColor=val; }
	public Color CreaseColor() { return FCreaseColor; }
	public void CreaseColor(Color val) { FCreaseColor=val; }
	public Color CreaseEdgeColor() { return FCreaseEdgeColor; }
	public void CreaseEdgeColor(Color val) { FCreaseEdgeColor=val; }
	public Color CurvaturePloColor() { return FCurvaturePlotColor; }
	public void CurvaturePloColor(Color val) { FCurvaturePlotColor=val; }
	public Color DiagonalColor() { return FDiagonalColor; }
	public void DiagonalColor(Color val) { FDiagonalColor=val; }
	public Color GridColor() { return FGridColor; } 
	public void GridColor(Color val) { FGridColor=val; }
	public Color GridFonColor() { return FGridFontColor; }
	public void GridFonColor(Color val) { FGridFontColor=val; }
	public Color HydrostaticsFonColor() { return FHydrostaticsFontColor; }
	public void HydrostaticsFonColor(Color val) { FHydrostaticsFontColor=val; }

	public File ImportDirectory() { return FGetImportDirectory(); }
	public void ImportDirectory(File val) { FImportDirectory=val; }
	public Color EdgeColor() { return FEdgeColor; }
	public void EdgeColor(Color val) { FEdgeColor=val; }
	public File ExportDirectory() { return FGetExportDirectory(); }
	public void ExportDirectory(File val) { FExportDirectory=val; }
	public Color CreasePoinColor() { return FCreasePointColor; }
	public void CreasePoinColor(Color val) { FCreasePointColor=val; }
	public String LanguageFile() { return FLanguageFile; }
	public void LanguageFile(String val) { FLanguageFile=val; }
	public Color LayerColor() { return FLayerColor; }
	public void LayerColor(Color val) { FLayerColor=val; }
	public Color LeakPoinColor() { return FLeakPointColor; }
	public void LeakPoinColor(Color val) { FLeakPointColor=val; }
	public Color MarkerColor() { return  FMarkerColor; }
	public void MarkerColor(Color val) {  FMarkerColor=val; }
	public int MaxUndoMemory() { return FMaxUndoMemory; }
	public void MaxUndoMemory(int val) { FMaxUndoMemory=val; }
	public Color NormalColor() { return FNormalColor; }
	public void NormalColor(Color val) { FNormalColor=val; }
	public File OpenDirectory()  { return FGetOpenDirectory(); }
	public void OpenDirectory(File val)  { FOpenDirectory=val; }
	public File SaveDirectory()  { return FGetSaveDirectory(); }
	public void SaveDirectory(File val)  { FSaveDirectory=val; }
	public Color StationColor()   { return FStationColor; }
	public void StationColor(Color val)   { FStationColor=val; }
	public Color UnderWaterColor() { return FUnderWaterColor; }
	public void UnderWaterColor(Color val) { FUnderWaterColor=val; }
	public Color RegularPoinColor() { return FRegularPointColor; }
	public void RegularPoinColor(Color val) { FRegularPointColor=val; }
	public Color DartPointColor()  { return FDartPointColor; }
	public void DartPointColor(Color val)  { FDartPointColor=val; }
	public Color SelectColor()  { return FSelectColor; }
	public void SelectColor(Color val)  { FSelectColor=val; }
	public Color ViewportColor()  { return FViewportColor; }
	public void ViewportColor(Color val)  { FViewportColor=val; }
	public Color WaterlineColor() { return FWaterlineColor; }
	public void WaterlineColor(Color val) { FWaterlineColor=val; }
	public Color ZebraStripeColor() { return FZebraStripeColor; }
	public void ZebraStripeColor(Color val) { FZebraStripeColor=val; }
}
